【今日湯底】
Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.
Examples:
spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw"
spinWords( "This is a test") => returns "This is a test"
spinWords( "This is another test" )=> returns "This is rehtona test"
(必須通過以下測試)
(ns clojure.spin-words-test
(:require [clojure.test :refer :all]
[clojure.spin-words :refer :all]))
(deftest one-word-test
(is (= (spin-words "Welcome") "emocleW")))
【我的答案】
(ns clojure.spin-words
(:require [clojure.string :as str]))
(defn spin-words [strng]
(->> (str/split strng #" ")
(map #(if (>= (count %) 5) (apply str (reverse %)) %))
(str/join " "))
)
【其他人的答案】
(ns clojure.spin-words
(:require [clojure.string :as string]))
(defn spin-words [s]
(string/replace s #"\w{5,}" string/reverse))